// index.js
// 获取应用实例
const app = getApp()
const utils = require("../../utils/network.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this
    console.log(22,options.category_id)
    wx.setNavigationBarTitle({
      title:options.category_name
    })
    wx.request({
      url: 'https://www.tianba.work/api/article/list/category_id/'+options.category_id,
      success(res){
        console.log('分类文章',res)
        that.setData({
          articleList:res.data.data.articleList,
          page:1,
          total:res.data.data.total
        })
              that.onReachBottom()
      }
    })
  },
  onReachBottom: function (){
    var that = this;
    wx.showLoading({
      title: '玩命加载中',
    })
    that.data.page = that.data.page+1;
    utils.request({
      url: 'https://www.tianba.work/api/article/list/category_id/'+that.options.category_id,
       method: 'GET',
       header: {'Content-Type': 'application/json' },
       data: {page:that.data.page},
       success: function(res) {
        console.log(res)
        const oldDate = that.data.articleList;
         if(res.data.status==0){
            that.setData({
              articleList : oldDate.concat(res.data.data.articleList),
            })
            wx.hideLoading();
         }
         if(that.data.total==that.data.articleList.length){
          console.log(333)
          wx.showToast({
            title: '无更多数据',
            icon: 'none'  
          });
          console.log(that.onReachBottom);
          that.onReachBottom = function(){}
        }
        }
    })

  },
  onPullDownRefresh: function () {
    let that =this
    that.onLoad(that.options)
    wx.stopPullDownRefresh() //刷新完成后停止下拉刷新动效
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */

  /**
   * 用户点击右上角分享
   */
 
  onShareAppMessage: function () {
    const { info, code } = that.data;
    return {
      title: info.name,
      path: `/pages/share/index?code=${code}&uid=${wx.getStorageSync('uid')}`,
      imageUrl: info.thumbnail_url
    }
  },
  onShareTimeline: function(){
    const { info, code } = that.data;
    return {
      title: info.name,
      query: `code=${code}&uid=${wx.getStorageSync('uid')}`,
      imageUrl: info.thumbnail_url
    }
  }
})